﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionePolveriLib.BL;

using System.Web.Security;

namespace GestionePolveriLib.Facades
{
    [Serializable]
    public class OperatoriFacade
    {
        private string m_errorMotivation = string.Empty;

        public string LastErrorMotivation
        {
            get { return m_errorMotivation; }
            set { m_errorMotivation = value; }
        }

        public OperatoriFacade()
        {

        }

        public List<Operatore> GetOperatori()
        {
            List<Operatore> retValue = null;

            try
            {
                MembershipUserCollection coll = Membership.GetAllUsers();
                if (coll.Count > 0)
                {
                    retValue = new List<Operatore>();
                    foreach (MembershipUser user in coll)
                    {
                        retValue.Add(new Operatore() { ID=Guid.Parse(user.ProviderUserKey.ToString()), UserName=user.UserName, Password="", Ruolo=RuoliFacade.GetRuoli(user.UserName) });
                    }
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }

            return retValue;
        }

        public bool AddOperatore(string userName, string password, List<Ruolo> ruoli)
        {
            bool retValue = false;
            MembershipCreateStatus m_membershipStatus;
            
            try
            {
                MembershipUser user = Membership.CreateUser(userName, password, "fimet@fimet.it", "Fimet", "Fimet", true, out m_membershipStatus);

                switch (m_membershipStatus)
                {
                    case MembershipCreateStatus.DuplicateEmail:
                        m_errorMotivation = "Email duplicata";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.DuplicateProviderUserKey:
                        m_errorMotivation = "chiave utente duplicata";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.DuplicateUserName:
                        m_errorMotivation = "Nome utente duplicato";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidAnswer:
                        m_errorMotivation = "Risposta segreta non valida";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidEmail:
                        m_errorMotivation = "Email non valida";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidPassword:
                        m_errorMotivation = "Email password non valida";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidProviderUserKey:
                        m_errorMotivation = "Chiave utente non valida";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidQuestion:
                        m_errorMotivation = "Domanda segreta non valida";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.InvalidUserName:
                        m_errorMotivation = "Username non valido";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.ProviderError:
                        m_errorMotivation = "Errore nell'accesso al sistema di autenticazione";
                        retValue = false;
                        break;
                    case MembershipCreateStatus.Success:
                        {
                            foreach (Ruolo r in ruoli)
                            {
                                if (Roles.RoleExists(r.Descrizione))
                                {
                                    Roles.AddUserToRole(userName, r.Descrizione);
                                }
                            }
                            retValue = true;
                        }
                        break;
                    case MembershipCreateStatus.UserRejected:
                        m_errorMotivation = "Registrazione utente rifiutata";
                        retValue = false;
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                retValue = false;
                // throw ex;
            }

            return retValue;
        }

        public bool DeleteOperatore(string userName)
        {
            bool retValue = false;

            try
            {
                string[] roleNames = Roles.GetRolesForUser(userName);
                Roles.RemoveUserFromRoles(userName, roleNames);

                retValue = Membership.DeleteUser(userName); 
            }
            catch (Exception ex)
            {
                retValue = false;
                // throw ex;
            }

            return retValue;
        }

        public bool ResetPassword(string newPassword, string userName)
        {
            bool retValue = false;

            try
            {
                MembershipUser user = Membership.GetUser(userName);
                string oldPassword = user.ResetPassword("Fimet");

                retValue = user.ChangePassword(oldPassword, newPassword);
            }
            catch (Exception ex)
            {
                // throw ex;
            }

            return retValue;
        }

        public bool Validate(string userName, string password)
        {
            bool retValue = false;

            try
            {
                if (Membership.ValidateUser(userName, password))
                {
                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }

            return retValue;
        }

        //public Operatore Validate(string userName, string password)
        //{
        //    Operatore retValue = null;

        //    try
        //    {
        //        if (Membership.ValidateUser(userName, password))
        //        {
        //            retValue = GetOperatore(userName);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        // throw ex;
        //    }

        //    return retValue;
        //}

        public Operatore GetOperatore(string userName)
        {
            Operatore retValue = null;

            try
            {
                MembershipUser user = Membership.GetUser(userName);

                if(user != null)
                {
                    retValue = new Operatore();
                    retValue.ID = Guid.Parse(user.ProviderUserKey.ToString());
                    retValue.UserName = userName;
                    retValue.Ruolo = RuoliFacade.GetRuoli(userName);
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }

            return retValue;
        }
    }
}
